package cn.myplus.blog.configure;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertySources;
import org.springframework.stereotype.Component;


/**
 * 获取apollo中取得的配置信息
 *
 * @author sunjf
 * @since 2020/9/11 13:26
 */
@Component
//@ConditionalOnProperty(prefix = "apollo", name = "bootstrap.enabled", havingValue = "true")
public class ConfigHolder implements ApplicationContextAware {
    private final Logger logger = LoggerFactory.getLogger(ConfigHolder.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
        PropertySources propertySources = context.getEnvironment().getPropertySources();
        propertySources.forEach(propertySource -> {
            String propertySourceName = propertySource.getName();
//            if(APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME.equals(propertySourceName) || APOLLO_PROPERTY_SOURCE_NAME.equals(propertySourceName) ){
//                CompositePropertySource ps = (CompositePropertySource)propertySource;
//                ps.getPropertySources().forEach(config->{
//                    ConfigPropertySource cps = (ConfigPropertySource) config;
//                    Stream.of(cps.getPropertyNames())
//                            .forEach(propertyName->logger.info("namespace {}:{}==>{}",
//                                    cps.getName(),
//                                    propertyName,
//                                    cps.getProperty(propertyName)));
//                });
//            }
        });
    }
}
